package com.gomtel.gomtelsdk;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.gomtel.smartdevice.api.BleManager;
import com.gomtel.smartdevice.api.MessageManager;
import com.gomtel.smartdevice.api.bean.AFDataResponse;
import com.gomtel.smartdevice.api.bean.AFResponse;
import com.gomtel.smartdevice.bt.bt.BluetoothChatService;
import com.gomtel.smartdevice.bt.bt.Constants;
import com.gomtel.smartdevice.interfaces.OnCallbackListener;
import com.gomtel.smartdevice.view.HeartBeatView;

public class AFActivity extends AppCompatActivity {

    private Button btnStart;
    private HeartBeatView heartBeatView;
    private BleManager bleManager = BleManager.getInstance();
    private MessageManager messageManager = MessageManager.getInstance();
    private TextView logView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hrv);
        btnStart = (Button) findViewById(R.id.btn_start);
        heartBeatView = (HeartBeatView) findViewById(R.id.heartBeatView);
        heartBeatView.setVisibility(View.GONE);
        logView = (TextView) findViewById(R.id.tv_info);
        logView.setMovementMethod(ScrollingMovementMethod.getInstance());

        //连接心电
        if (bleManager.getStatus() == BleManager.CONNECT_STATUS_CONNECTED)
            messageManager.connectAF(MainActivity.MAC,new OnCallbackListener<AFDataResponse>() {
                @Override
                public void onCallback(AFDataResponse afDataResponse) {
                    switch (afDataResponse.getType()) {
                        case Constants.MESSAGE_STATE_CHANGE:
                            if (afDataResponse.getStatus() == BluetoothChatService.STATE_CONNECTED) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        ToastUtil.showShort(AFActivity.this, "房颤测量准备完成");
                                        refreshLogView("房颤测量准备完成");
                                        heartBeatView.setVisibility(View.VISIBLE);
                                    }
                                });

                            }
                            break;
                        case Constants.MESSAGE_READ:
                            if (afDataResponse.getMv() != 0 && afDataResponse.getTimeStamp() != 0) {
                                //显示心电图
                                heartBeatView.addPPG(afDataResponse.getMv(), afDataResponse.getTimeStamp());
                            }
                            break;
                    }
                }
            });
        else {
            ToastUtil.showShort(this, "请先连接蓝牙");
            refreshLogView("请先连接蓝牙");
        }
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageManager.getInstance().sendTriggerMeasurement(MessageManager.MeasurementID_AF, MessageManager.MeasurementID_STATUS_START);
            }
        });

        //心电的数据回调
        messageManager.setAFListener(new OnCallbackListener<AFResponse>() {
            @Override
            public void onCallback(AFResponse hrvBean) {
                refreshLogView("PPI = " + hrvBean.getPpi() + " State = " + hrvBean.getState()
                        + "  UnixTimestamp = " + hrvBean.getUnixTimestamp());
            }
        });

    }


    public void refreshLogView(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                logView.append("\n-----------------------------");
                logView.append("\n" + msg);
                int offset = logView.getLineCount() * logView.getLineHeight();
                if (offset > logView.getHeight()) {
                    logView.scrollTo(0, offset - logView.getHeight());
                }
            }
        });

    }
}
